package cn.iocoder.yudao.framework.mqtt.core.annotation;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MqttPublisher {
    private final MqttClient mqttClient;

    @Autowired
    public MqttPublisher(MqttClient mqttClient) {
        this.mqttClient = mqttClient;
    }

    public void publish(String topic, String payload) {
        try {
            MqttMessage message = new MqttMessage(payload.getBytes());
            message.setQos(2); // 设置 QoS 等级
            mqttClient.publish(topic, message);
            System.out.println("Message published to topic: " + topic);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}
